package LeetCode.exercise;

import java.util.ArrayList;
import java.util.List;

public class LCR085 {
    List<String> res = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    int left = 0, right = 0;
    public List<String> generateParenthesis(int n) {
        bfs(n);
        return res;
    }
    public void bfs(int n){
        if(sb.length() == 2 * n){
            res.add(sb.toString());
            return;
        }
        if(left < n){
            sb.append('(');
            left++;
            bfs(n);
            sb.deleteCharAt(sb.length() -1);
            left--;
        }
        if(right < left){
            sb.append(')');
            right++;
            bfs(n);
            sb.deleteCharAt(sb.length() -1);
            right--;
        }
    }

    public static void main(String[] args) {
        System.out.println(new LCR085().generateParenthesis(3));
    }
}
